package com.neusoft.elmboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neusoft.elmboot.Dto.UserDto;
import com.neusoft.elmboot.Vo.UserVo;
import com.neusoft.elmboot.common.constant.JwtClaimsConstant;
import com.neusoft.elmboot.common.constant.MessageConstant;
import com.neusoft.elmboot.common.constant.StatusConstant;
import com.neusoft.elmboot.common.exception.AccountLockedException;
import com.neusoft.elmboot.common.exception.AccountNotFoundException;
import com.neusoft.elmboot.common.exception.BaseException;
import com.neusoft.elmboot.common.exception.PasswordErrorException;
import com.neusoft.elmboot.common.properties.JwtProperties;
import com.neusoft.elmboot.common.utils.JwtUtil;
import com.neusoft.elmboot.mapper.UserMapper;
import com.neusoft.elmboot.po.UserEntity;
import com.neusoft.elmboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private JwtProperties jwtProperties;

    public UserVo login(UserDto userDto) {
        String username = userDto.getUserName();
        String password = userDto.getPassword();
        /*LambdaQueryWrapper<UserEntity> lqw = new LambdaQueryWrapper();
        lqw.eq(UserEntity::getUserName, username);*/
        UserEntity userEntity = userMapper.getByUserName(userDto);
        if (userEntity == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);

        }
        //对密码进行加密处理
        //password = DigestUtils.md5DigestAsHex(password.getBytes());

        if (!userEntity.getPassword().equals(password)){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //查看是否被锁定
        if (userEntity.getDel()== StatusConstant.ENABLE){
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }

        //生成jwt令牌
        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, userEntity.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        /*UserVo.builder().userName(username).userId(userEntity.getUserId())
                .userSex((userEntity.getUserSex())*/
        UserVo userVo = new UserVo();
        userVo.setUserName(username);
        userVo.setUserId(userEntity.getUserId());
        userVo.setToken(token);
        userVo.setUserSex(userEntity.getUserSex().toString());






        return userVo;
    }

    @Override
    public void add(UserDto userDto) {
        LambdaQueryWrapper<UserEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(userDto.getUserName()!=null,UserEntity::getUserName,userDto.getUserName());
        UserEntity user = this.getOne(lqw);
        if (user!=null){
            throw new BaseException("用户名已存在");
        }


        UserEntity userEntity = new UserEntity();
        BeanUtils.copyProperties(userDto,userEntity);

        this.save(userEntity);
    }

    @Override
    public UserEntity getByUserName(UserDto userDto) {
        return userMapper.getByUserName(userDto);


    }
}

